package com.ygqh.baby.service.impl;

import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.YgProductCombinationMapper;
import com.ygqh.baby.model.ProductCombinationEditRequest;
import com.ygqh.baby.model.ProductCombinationModel;
import com.ygqh.baby.model.ProductCombinationSearchRequest;
import com.ygqh.baby.po.YgProductCombination;
import com.ygqh.baby.po.YgProductCombinationExample;
import com.ygqh.baby.service.YgProductCombinationDetailService;
import com.ygqh.baby.service.YgProductCombinationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author admin
 */
@Service
public class YgProductCombinationServiceImpl implements YgProductCombinationService {

    @Autowired
    private YgProductCombinationMapper ygProductCombinationMapper;

    @Autowired
    private YgProductCombinationDetailService ygProductCombinationDetailService;

    @Override
    public List<YgProductCombination> find() {
        YgProductCombinationExample example = new YgProductCombinationExample();
        example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
        return ygProductCombinationMapper.selectByExample(example);
    }


    @Override
    public YgProductCombination findById(Long id) {
        return ygProductCombinationMapper.selectByPrimaryKey(id);
    }


    private int save(ProductCombinationEditRequest editRequest) {
        YgProductCombination productCombination = new YgProductCombination();
        productCombination.setName(editRequest.getName());
        productCombination.setLinkUrl(editRequest.getLinkUrl());
        productCombination.setCreateBy(editRequest.getHandler());
        productCombination.setCreateTime(new Date());
        productCombination.setUpdateBy(editRequest.getHandler());
        productCombination.setUpdateTime(productCombination.getCreateTime());

        int i = ygProductCombinationMapper.insertSelective(productCombination);
        ygProductCombinationDetailService.saveOrUpdate(productCombination.getId(), editRequest.getProductIds(),
                editRequest.getHandler(), productCombination.getCreateTime());
        return i;
    }

    private int update(ProductCombinationEditRequest editRequest) {
        YgProductCombination productCombination = new YgProductCombination();
        productCombination.setId(editRequest.getProductCombinationId());
        productCombination.setName(editRequest.getName());
        productCombination.setLinkUrl(editRequest.getLinkUrl());
        productCombination.setUpdateBy(editRequest.getHandler());
        productCombination.setUpdateTime(productCombination.getCreateTime());
        int i = ygProductCombinationMapper.updateByPrimaryKeySelective(productCombination);
        ygProductCombinationDetailService.saveOrUpdate(productCombination.getId(), editRequest.getProductIds(),
                editRequest.getHandler(), productCombination.getCreateTime());
        return i;
    }

    @Override
    public ResultSet<YgProductCombination> search(ProductCombinationSearchRequest searchRequest) {
        int count = ygProductCombinationMapper.countSuper(searchRequest);
        List<YgProductCombination> list = ygProductCombinationMapper.selectSuper(searchRequest);
        ResultSet<YgProductCombination> resultSet = new ResultSet<>(count, list);
        return resultSet;
    }

    @Override
    public ProductCombinationModel findInfo(Long productCombinationId) {
        return ygProductCombinationMapper.selectProductCombinationInfo(productCombinationId);
    }

    @Override
    public int delete(Long productCombinationId, String deleteBy) {
        YgProductCombination combination = new YgProductCombination();
        combination.setId(productCombinationId);
        combination.setUpdateTime(new Date());
        combination.setUpdateBy(deleteBy);
        combination.setStatus(DataStatus.Delete.name());
        int i = ygProductCombinationMapper.updateByPrimaryKeySelective(combination);
        ygProductCombinationDetailService.delete(productCombinationId, null, deleteBy, combination.getUpdateTime());
        return i;
    }

    @Override
    public int saveOrUpdate(ProductCombinationEditRequest editRequest) {
        int r;
        if (editRequest.getProductCombinationId() != null) {
            r = this.update(editRequest);
        } else {
            r = this.save(editRequest);
        }
        return r;
    }

}

